//
//  WeiboCellLayout.m
//  weibo-01
//
//  Created by HZApple on 16/5/11.
//  Copyright © 2016年 hdu. All rights reserved.
//

#import "WeiboCellLayout.h"
#import "WXLabel.h"

#define KCellOringinHeight 60
#define KSpace 10

@implementation WeiboCellLayout

- (NSMutableArray *)weiboImgFrameArray {
    
    if (_weiboImgFrameArray == nil) {
        _weiboImgFrameArray = [NSMutableArray array];
    }
    
    return  _weiboImgFrameArray;
    
}


- (void)setWeibo:(WeiboModel *)weibo {
    
    _weibo = weibo;
    

        self.cellHeight += KCellOringinHeight;
        
       // NSDictionary *attributes = @{
        //                             NSFontAttributeName : [UIFont systemFontOfSize:16]
        //                             };
        //CGRect weiboTextRect = [_weibo.text boundingRectWithSize:CGSizeMake(kScreenWidth - 20, 9999) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:NULL];
        
    
//        self.weiboTextFrame = CGRectMake(KSpace, KCellOringinHeight + KSpace, kScreenWidth - 20, weiboTextRect.size.height);
      CGFloat weiboTextHeight = [WXLabel getTextHeight:16 width:kScreenWidth - 20 text:self.weibo.text];
    //根据高度来设定正文label的frame
       self.weiboTextFrame = CGRectMake(kSpace, kCellOriginHeight + kSpace, kScreenWidth - 20, weiboTextHeight);
    
    
        self.cellHeight += CGRectGetHeight(self.weiboTextFrame) + KSpace * 2;
    
    
        if (_weibo.pic_urls != nil) {
            
            if (_weibo.retweeted_status == nil) {
                CGFloat imgsHeight = [self weiboMultiImagesFrame:self.weiboTextFrame picUrls:self.weibo.pic_urls];
                self.cellHeight += imgsHeight;
            }
            //设置转发时自己的图片

        }
    
        [self reWeiboFrames];
    
        // NSLog(@"%li",self.weiboImgFrameArray.count);
    
        for (NSInteger i = self.weiboImgFrameArray.count; i < kImagesCount; i++) {
          
            [self.weiboImgFrameArray addObject:[NSValue valueWithCGRect:CGRectZero]];
            
        }

}

- (CGFloat)weiboMultiImagesFrame:(CGRect)imgRefFrame picUrls:(NSArray *)urls {
    
    //1.确定第一张图片的X,Y
    CGFloat imgX = CGRectGetMinX(imgRefFrame);
    CGFloat imgY = CGRectGetMaxY(imgRefFrame) + kSpace;
    
    //2.确定图片的宽高
    CGFloat imgSize = (CGRectGetWidth(imgRefFrame) - 2 * kImageGap) / kImagesCountPerLine;
    
    //3.计算多个图片的frame
    
    NSInteger row = 0;
    NSInteger column = 0;
    
    for (NSInteger i = 0; i < urls.count; i++) {
        
        row = i / kImagesCountPerLine;
        column = i % kImagesCountPerLine;
        
        CGRect imgFrame = CGRectMake(imgX + column * (imgSize + kImageGap), imgY + row *(imgSize + kImageGap), imgSize, imgSize);

        [self.weiboImgFrameArray addObject:[NSValue valueWithCGRect:imgFrame]];
        
    }


    NSInteger line = (urls.count + kImagesCountPerLine - 1)/  kImagesCountPerLine;

    NSInteger imgGap = MAX(line - 1, 0);

    NSInteger imgTextGap = MIN(1, MAX(0, line));
    
    CGFloat imgsHeight = line * imgSize + imgGap * kImageGap + imgTextGap * kSpace;

    return imgsHeight;
    

}

- (void)reWeiboFrames {
    
    self.reWeiboBgImgFrame = CGRectZero;
    self.reWeiboTextFrame = CGRectZero;
    
    if (_weibo.retweeted_status != nil) {
        
        //1.转发微博的背景frame
        self.reWeiboBgImgFrame = CGRectMake(CGRectGetMinX(self.weiboTextFrame), CGRectGetMaxY(self.weiboTextFrame) + kSpace, CGRectGetWidth(self.weiboTextFrame), 0);
        
        //2.转发微博正文的frame
//        CGRect reWeiboTextRect = [_weibo.retweeted_status.text boundingRectWithSize:CGSizeMake(CGRectGetWidth(self.reWeiboBgImgFrame) - 2 *kSpace, 9999) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:15]} context:NULL];
//        
        
        CGFloat reWeiboTextHeight = [WXLabel getTextHeight:15 width:kScreenWidth - 20 text:self.weibo.retweeted_status.text];
        self.reWeiboTextFrame = CGRectMake(CGRectGetMinX(self.reWeiboBgImgFrame) + kSpace, CGRectGetMinY(self.reWeiboBgImgFrame) + kSpace,CGRectGetWidth(self.reWeiboBgImgFrame) - 2 *kSpace, reWeiboTextHeight);
        
        
        CGFloat bgHeight = reWeiboTextHeight + kSpace;
        
        
        //3.如果转发微博有图片
        if (_weibo.retweeted_status.thumbnail_pic != nil) {
            
//            self.weiboImgFrame = CGRectMake(CGRectGetMinX(self.reWeiboTextFrame), CGRectGetMaxY(self.reWeiboTextFrame) + kSpace, 100, 100);
            CGFloat imgsHeight = [self weiboMultiImagesFrame:self.reWeiboTextFrame picUrls:self.weibo.retweeted_status.pic_urls];
            //self.cellHeight += imgsHeight;
            bgHeight += imgsHeight;
            
           // bgHeight += CGRectGetHeight(self.weiboImgFrame) + 2 * kSpace;
            
        }
        
        bgHeight += kSpace;
        
        //4.重新设置背景图片的高度
        self.reWeiboBgImgFrame = CGRectMake(CGRectGetMinX(self.weiboTextFrame), CGRectGetMaxY(self.weiboTextFrame) + kSpace, CGRectGetWidth(self.weiboTextFrame), bgHeight);
        
        self.cellHeight += CGRectGetHeight(self.reWeiboBgImgFrame) + kSpace;
        
    }

    
}
@end
